<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class LoginController extends Controller{
    //后台首页
    public function index(){
    	
    	
    	if(IS_POST){
    		
    		//判断验证码是否是正确的
	    	$c=$_SESSION['code'];
	    	$code=Q('post.code',null,'strtoupper');
	//		p($code);
	//		exit;
			if($code!=$c) View::error('验证码错误');
			//判断用户名是否存在
			$username=Q('post.username');
			$data=Db::table('user')->where("username='{$username}'")->get();
			if(!$data) View::error('用户名错误');
//			exit;
			//判断密码是否正确
			$password=md5(Q('post.password'));
			$dapassword=$data[0]['password'];
			$dauid=$data[0]['uid'];
			if($password!=$dapassword) View::error('密码错误');
			
			//登录成功
			$_SESSION=[
				'username'=>$username,
				'uid'=>$dauid
			];
			View::success('登录成功',U('Index/index'));
    	}
    	//显示登录页面
        View::make();
    }
	public function code(){
	    Code::width(125)->height(42)->num(C("webSet.CODE_LEN"))->fontColor(C('webSet.CODE_BG'))->make();
	}
	//退出登录
	public function out(){
		session_unset();
	    session_destroy();
		go(U('index'));
		exit;
	}
	//验证异步
	public function ajax_o(){
	    $old_psd=md5($_POST['new_pas']);
		$username = $_SESSION['username'];
		$pwd=Db::table('user')->where("username='{$username}'")->get();
		if($old_psd != $pwd[0]['password']){
			echo 1;
		}
	}			
}























































